# --------------------------------------------------------
# X-Decoder -- Generalized Decoding for Pixel, Image, and Language
# Copyright (c) 2022 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Xueyan Zou (xueyan@cs.wisc.edu)
# --------------------------------------------------------

import os
import sys
import logging

pth = '/'.join(sys.path[0].split('/')[:-1])
sys.path.insert(0, pth)

from PIL import Image
import numpy as np
np.random.seed(0)
# import cv2

import torch
import torch.nn.functional as F
from torchvision import transforms

from utils.arguments import load_opt_command

from detectron2.data import MetadataCatalog
from detectron2.structures import BitMasks
from xdecoder.BaseModel import BaseModel
from xdecoder import build_model
from detectron2.utils.colormap import random_color
from utils.visualizer import Visualizer
from utils.distributed import init_distributed

logger = logging.getLogger(__name__)


def main(args=None):
    '''
    Main execution point for PyLearn.
    '''
    opt, cmdline_args = load_opt_command(args)
    if cmdline_args.user_dir:
        absolute_user_dir = os.path.abspath(cmdline_args.user_dir)
        opt['base_path'] = absolute_user_dir
    opt = init_distributed(opt)

    # META DATA
    pretrained_pth = os.path.join(opt['WEIGHT']) # suggest using no VG version.
    if 'novg' not in pretrained_pth:
        assert False, "Using the ckpt without visual genome training data will be much better."
    output_root = './output'
    image_pth = 'images/landscape.jpg'
    text = 'water'

    model = BaseModel(opt, build_model(opt)).from_pretrained(pretrained_pth).eval().cuda()
    model.model.sem_seg_head.predictor.lang_encoder.get_text_embeddings(["background"], is_eval=False)

    t = []
    t.append(transforms.Resize(224, interpolation=Image.BICUBIC))
    transform_ret = transforms.Compose(t)
    t = []
    t.append(transforms.Resize(512, interpolation=Image.BICUBIC))
    transform_grd = transforms.Compose(t)
 
    metedata = MetadataCatalog.get('coco_2017_train_panoptic')
    
    with torch.no_grad():
        image_ori = Image.open(image_pth).convert("RGB")
        width = image_ori.size[0]
        height = image_ori.size[1]
        image = transform_grd(image_ori)
        image = np.asarray(image)
        images = torch.from_numpy(image.copy()).permute(2,0,1).cuda()

        batch_inputs = [{'image': images, 'groundings': {'texts':[[text]]}, 'height': height, 'width': width}]
        outputs = model.model.evaluate_grounding(batch_inputs, None)

        grd_mask = (outputs[-1]['grounding_mask'] > 0).float()
        grd_mask_ = (1 - F.interpolate(grd_mask[None,], (224, 224), mode='nearest')[0]).bool()
        grd_mask_ = grd_mask_ * 0

        image_ori = Image.open(image_pth).convert("RGB")
        image = transform_ret(image_ori)
        image_ori = np.asarray(image_ori)
        image = np.asarray(image)
        images = torch.from_numpy(image.copy()).permute(2,0,1).cuda()
        batch_inputs = [{'image': images, 'image_id': 0, 'captioning_mask': grd_mask_}]
        outputs = model.model.evaluate_captioning(batch_inputs)

        visual = Visualizer(image_ori, metadata=metedata)
        color = [252/255, 91/255, 129/255]
        text = outputs[-1]['captioning_text']
        
        demo = visual.draw_binary_mask(grd_mask.cpu().numpy()[0], color=color, text=text)
        demo.save(os.path.join(output_root, 'ref_cap.png'))

if __name__ == "__main__":
    main()
    sys.exit(0)